//
//  ConnectTestPage.swift
//  xgwjinterconnection
//
//  Created by WangWeiShuo on 2025/8/29.
//


import SwiftUI

struct ConnectTestPage: View {
@Environment(\.dismiss) var dismiss
// 引用Socket管理器
    @ObservedObject private var socketManager = SocketManager.shared
    
    // 输入字段
    @State private var host = "192.168.100.100"
    @State private var port = "8080"
    @State private var message = ""

var body: some View {
    ZStack{
        VStack{
            WikaNavigationView(showBackIcon: false, title: "设备连接",onBack: {
                dismiss() }).frame(maxHeight: 65)
            
            // 服务器连接设置
                        VStack(spacing: 8) {
                            Text("服务器设置")
                                .font(.headline)
                            
                            HStack {
                                TextField("主机地址", text: $host)
                                    .textFieldStyle(RoundedBorderTextFieldStyle())
                                
                                TextField("端口", text: $port)
                                    .textFieldStyle(RoundedBorderTextFieldStyle())
                                    .frame(width: 100)
                            }
                            
                            HStack {
                                Button(action: connect) {
                                    Text("连接")
                                        .frame(maxWidth: .infinity)
                                }
                                .buttonStyle(.borderedProminent)
                                .disabled(socketManager.state == .connecting || socketManager.state == .connected)
                                
                                Button(action: disconnect) {
                                    Text("断开")
                                        .frame(maxWidth: .infinity)
                                }
                                .buttonStyle(.bordered)
                                .tint(.red)
                                .disabled(socketManager.state != .connected)
                            }
                        }
                        .padding()
                        .background(Color(.systemGray6))
                        .cornerRadius(10)
                        
                        // 连接状态
                        Text("状态: \(connectionStateText)")
                            .foregroundColor(connectionStateColor)
                        
                        // 消息列表
                        ScrollView {
                            VStack(alignment: .leading, spacing: 8) {
                                ForEach(socketManager.messages, id: \.self) { msg in
                                    Text(msg)
                                        .padding(8)
                                        .background(Color(.systemGray5))
                                        .cornerRadius(4)
                                }
                            }
                            .padding()
                        }
                        .background(Color(.systemGray6))
                        .cornerRadius(10)
                        
                        // 消息输入和发送
                        HStack {
                            TextField("输入消息...", text: $message)
                                .textFieldStyle(RoundedBorderTextFieldStyle())
                            
                            Button(action: sendMessage) {
                                Image(systemName: "paperplane")
                                    .padding()
                                    .background(socketManager.state == .connected ? Color.blue : Color.gray)
                                    .foregroundColor(.white)
                                    .cornerRadius(50)
                            }
                            .disabled(socketManager.state != .connected || message.isEmpty)
                        }
        }
    }
    .navigationBarBackButtonHidden(true)
    .enableSwipeBackGesture()
}

// 连接状态文本
    private var connectionStateText: String {
        switch socketManager.state {
        case .disconnected:
            return "未连接"
        case .connecting:
            return "连接中..."
        case .connected:
            return "已连接"
        case .error(let error):
            return "错误: \(error)"
        }
    }
    
    // 连接状态颜色
    private var connectionStateColor: Color {
        switch socketManager.state {
        case .disconnected:
            return .gray
        case .connecting:
            return .orange
        case .connected:
            return .green
        case .error:
            return .red
        }
    }
    
    // 连接到服务器
    private func connect() {
        guard let portNumber = UInt16(port) else {
            socketManager.messages.append("无效的端口号")
            return
        }
        socketManager.connect(host: host, port: portNumber)
    }
    
    // 断开连接
    private func disconnect() {
        socketManager.disconnect()
    }
    
    // 发送消息
    private func sendMessage() {
        socketManager.sendMessage(message)
        message = ""
    }
}

#Preview {
    ConnectTestPage()
}
